/*
 * Copyright(c) 2018-? the original author[Wien(luweien@aliyun.com)] or authors.
 *
 */
package org.doll.it.sequence.service;

import org.doll.it.base.util.AssertUtils;
import org.doll.it.sequence.enums.BusinessTypeEnum;
import org.doll.it.sequence.impl.TwitterSnowFlake;
import org.doll.it.sequence.vo.SnowFlakeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ID生成器
 * 
 * @author Wien(luweien@aliyun.com)
 *
 */
@Service
public class DollSequenceService {

	@Autowired
	private TwitterSnowFlake snowFlake;

	/**
	 * 根据业务类型生成对应的Id
	 * @param busincessEnum 业务类型
	 * @return long型有序id
	 */
	public long nextId(BusinessTypeEnum businessType) {
		AssertUtils.notNull(businessType, "'BussinessTypeEnum' must not be null.");
		long id = snowFlake.nextId(businessType.getCode());
		TwitterSnowFlake.verifyId(id);
		return id;
	}

	/**
	 * 生成默认业务类型的Id
	 * @return long型有序id
	 */
	public long nextId() {
		return nextId(BusinessTypeEnum.DEFAULT);
	}

	/**
	 * 反解SnowFlake Id
	 * @param snowflakeId
	 * @return
	 */
	public SnowFlakeVO parseId(long snowflakeId) {
		return TwitterSnowFlake.parse(snowflakeId);
	}

}
